<?php
define( "HOST_NAME", "localhost", true );
define( "USER_NAME", "root", true );
define( "PASSWORD", "admin123", true );
define( "DATABASE", "ysmsdb", true );

abstract class DAO
{
    function ExecuteQuery($query){
    	$arr = array();
		mysql_connect(HOST_NAME, USER_NAME, PASSWORD) or die("Error 1");
		mysql_select_db(DATABASE);

		if ($result = mysql_query($query)) {
        	while( $row = mysql_fetch_array($result) ) {
        		array_push($arr, $row);
        	}
		} 
		if(mysql_error()){
			error_log(mysql_error());
		}
		mysql_close();
		return $arr;
	}
	
	function ExecuteInsert($query){
		$i = 0;
		mysql_connect(HOST_NAME, USER_NAME, PASSWORD) or die("Error 1");
		mysql_select_db(DATABASE);

		if( mysql_query($query) ) {
			# get last updated id
			$i = mysql_insert_id();
		}
		if(mysql_error()){
			error_log(mysql_error());
			error_log($query);
		}
		mysql_close();
		return $i;
	}

	function ExecuteUpdate($query){
		$i = 0;
		mysql_connect(HOST_NAME, USER_NAME, PASSWORD) or die("Error 1");
		mysql_select_db(DATABASE);
		
		if( mysql_query($query) )  {
			# get last updated id
			$i = mysql_affected_rows();
		}
		error_log(mysql_error());
		mysql_close();
		return $i;
	}

    public function get($id)
    {
		$object = null;
    	$result = $this->getList("id = $id");
		if(sizeof($result)>0){
			$object = $result[0];
		}
		return $object;
    }

//	abstract public function get($id);
	abstract public function save($object);
	abstract public function add($object);
	abstract public function delete($object);
	abstract public function getList($condition);
	abstract public function updateList($statement, $condition);
}

class School
{
	private $id;
	private $pic;
	private $name;
	private $description;
	private $picname;

    public function getDescription()
    {
        return $this->description;
    }
    public function setDescription($description)
    {
        $this->description = $description;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getName()
    {
        return $this->name;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
    public function getPic()
    {
        return $this->pic;
    }
    public function setPic($pic)
    {
        $this->pic = $pic;
    }

    public function getPicname()
    {
        return $this->picname;
    }
    public function setPicname($picname)
    {
        $this->picname = $picname;
    }

}

class SchoolDAO extends DAO {
	
	const tablename = "school";
	const fieldnames = "pic,name,description";

    public function add($object)
    {
		$pic = $object->getPic();
		$name = $object->getName();
		$description = $object->getDescription();
		$query = "INSERT INTO " . self::tablename . " (" . self::fieldnames . ") VALUES ( " .
		"$pic,'$name','$description')";
		
		$id = $this->ExecuteInsert($query);
		$object->setId($id);
    }
    public function delete($object)
    {
 		$query = "DELETE FROM " . self::tablename . " WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			exit;
		}
		// Use view HACK
		$query = "SELECT id, " . self::fieldnames . ",picname FROM vw_" . 
				 self::tablename . " WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new School();
        	$object->setId($row[0]);
			$object->setPic($row[1]);
			$object->setName($row[2]);
			$object->setDescription($row[3]);
			$object->setPicname($row[4]);
			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
		$id = $object->getId();
		$pic = $object->getPic();
		$name = $object->getName();
		$description = $object->getDescription();
		$query = "UPDATE " . self::fieldnames . " SET " .
		"pic='$pic', " . 
		"name='$name', " . 
		"description='$description' " . 
		"WHERE id=$id";
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE " . self::tablename . " SET " . $statement .
		" WHERE " . $condition;
		echo $query;
		return $this->ExecuteUpdate($query);
    }

}

class Semester{
	private $id;
	private $name;
	private $year;
	private $fromdate;
	private $todate;
	private $schoolid;

    public function getFromdate()
    {
        return $this->fromdate;
    }
    public function setFromdate($fromdate)
    {
        $this->fromdate = $fromdate;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getName()
    {
        return $this->name;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
    public function getTodate()
    {
        return $this->todate;
    }
    public function setTodate($todate)
    {
        $this->todate = $todate;
    }
    public function getYear()
    {
        return $this->year;
    }
    public function setYear($year)
    {
        $this->year = $year;
    }

    public function getSchoolid()
    {
        return $this->schoolid;
    }
    public function setSchoolid($schoolid)
    {
        $this->schoolid = $schoolid;
    }

}
class SemesterDAO extends DAO {
	
	const tablename = "semester";
	const fieldnames = "name,year,fromdate,todate";
	
    public function add($object)
    {
		$name = $object->getName();
		$year = $object->getYear();
		$fromdate = $object->getFromdate();
		$todate = $object->getTodate();
		$schoolid = $object->getSchoolid();
		
		$query = "INSERT INTO ".SemesterDAO::tablename." (".SemesterDAO::fieldnames.") VALUES ( " .
		"'$name','$year','$fromdate','$todate')";
		
		$id = $this->ExecuteInsert($query);
		$object->setId($id);
		
		// Insert into School_Semester
		$query = "INSERT INTO School_Semester(schoolid,semesterid) VALUES($schoolid,$id)";
		$this->ExecuteInsert($query);
    }
    public function delete($object)
    {
    	// Delete School_Semester
 		$query = "DELETE FROM School_Semester WHERE semesterid=" . $object->getId();
		$this->ExecuteUpdate($query);
		
 		$query = "DELETE FROM ".SemesterDAO::tablename." WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			exit;
		}
		$query = "SELECT id,".SemesterDAO::fieldnames.",schoolid FROM vw_".SemesterDAO::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new Semester();
        	$object->setId($row[0]);
			$object->setName($row[1]);
			$object->setYear($row[2]);
			$object->setFromdate($row[3]);
			$object->setTodate($row[4]);
			$object->setSchoolid($row[5]);
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
		$id = $object->getId();
		$name = $object->getName();
		$year = $object->getYear();
		$fromdate = $object->getFromdate();
		$todate = $object->getTodate();
		$query = "UPDATE ".SemesterDAO::tablename." SET " .
		"name='$name', " . 
		"year='$year', " . 
		"fromdate='$fromdate', " . 
		"todate='$todate' "  . 
		"WHERE id=$id";
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".SemesterDAO::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }

}

class User {
	private $id;
	private $name;
	private $login;
	private $password;
	private $schoolid;
	private $gid;
	private $gname;

    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getLogin()
    {
        return $this->login;
    }
    public function setLogin($login)
    {
        $this->login = $login;
    }
    public function getName()
    {
        return $this->name;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
    public function getPassword()
    {
        return $this->password;
    }
    public function setPassword($password)
    {
        $this->password = $password;
    }

    public function getGid()
    {
        return $this->gid;
    }
    public function setGid($gid)
    {
        $this->gid = $gid;
    }
    public function getGname()
    {
        return $this->gname;
    }
    public function setGname($gname)
    {
        $this->gname = $gname;
    }

    public function getSchoolid()
    {
        return $this->schoolid;
    }
    public function setSchoolid($schoolid)
    {
        $this->schoolid = $schoolid;
    }

}
class UserDAO extends DAO {
	const tablename = "user";
	const fieldnames = "name,login,password,schoolid";
	
    public function add($object)
    {
		$name = $object->getName();
		$login = $object->getLogin();
		$password = $object->getPassword();
		$schoolid = $object->getSchoolid();
		$gid = $object->getGid();
		$gname = $object->getGname();
		
		$query = "INSERT INTO ".self::tablename." (".self::fieldnames.") VALUES ( " .
		"'$name','$login', MD5('$password'), $schoolid )";
		
		$id = $this->ExecuteInsert($query);
		$object->setId($id);
		
		$query = "INSERT INTO usergroup_user(groupid, userid) VALUES($gid,$id)";
		$this->ExecuteInsert($query);
    }
    public function delete($object)
    {
 		$query = "DELETE FROM ".self::tablename." WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$query = "DELETE FROM usergroup_user WHERE userid=" . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			exit;
		}
		$query = "SELECT id,".self::fieldnames.",gid,gname FROM vw_".self::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new User();
        	$object->setId($row[0]);
			$object->setName($row[1]);
			$object->setLogin($row[2]);
			$object->setPassword($row[3]);
			$object->setSchoolid($row[4]);
			$object->setGid($row[5]);
			$object->setGname($row[6]);
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
		$id = $object->getId();
		$name = $object->getName();
		$login = $object->getLogin();
		$password = $object->getPassword();
		$schoolid = $object->getSchoolid();
		$query = "UPDATE ".self::tablename." SET " .
		"name='$name', " . 
		"login='$login', " . 
		"password='$password', "  .
		"schoolid = $schoolid" . 
		"WHERE id=$id";
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".self::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }

}

class Notice {
	private $id;
	private $subject;
	private $content;
	private $cdate;
	private $pageid;
	private $schoolid;
	private $referenceid;
	
    public function getCdate()
    {
        return $this->cdate;
    }
    public function setCdate($cdate)
    {
        $this->cdate = $cdate;
    }
    public function getContent()
    {
        return $this->content;
    }
    public function setContent($content)
    {
        $this->content = $content;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getPageid()
    {
        return $this->pageid;
    }
    public function setPageid($pageid)
    {
        $this->pageid = $pageid;
    }
    public function getSchoolid()
    {
        return $this->schoolid;
    }
    public function setSchoolid($schoolid)
    {
        $this->schoolid = $schoolid;
    }
    public function getSubject()
    {
        return $this->subject;
    }
    public function setSubject($subject)
    {
        $this->subject = $subject;
    }

    public function getReferenceid()
    {
        return $this->referenceid;
    }
    public function setReferenceid($referenceid)
    {
        $this->referenceid = $referenceid;
    }

}

class NoticeDAO extends DAO {
	const tablename = "notice";
	const fieldnames = "subject,content,cdate,pageid";
	
    /**
     * 
     * @return null
     * @param object $object
     */
	public function add($object)
    {
		$subject = $object->getSubject();
		$content = $object->getContent();
		$cdate = $object->getCdate();
		$pageid = $object->getPageid();
		$schoolid = $object->getSchoolid();
		$referenceid = $object->getReferenceid();
		
		$content = str_replace("'", "\'", $content);
		
		$query = "INSERT INTO ".self::tablename." (".self::fieldnames.") VALUES ( " .
		"'$subject','$content', now(), $pageid )";
		
		$id = $this->ExecuteInsert($query);
		$object->setId($id);

		$query = "INSERT INTO school_notice (schoolid,noticeid) VALUES ( " .
		"$schoolid,$id )";
		$this->ExecuteInsert($query);

		$query = "INSERT INTO notice_reference(noticeid,referenceid) VALUES($id,$referenceid)";
		$this->ExecuteInsert($query);
    }
    public function delete($object)
    {
 		$query = "DELETE FROM ".self::tablename." WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$query = "DELETE FROM school_notice WHERE noticeid=" . $object->getId();
		$this->ExecuteUpdate($query);
		$query = "DELETE FROM notice_reference WHERE noticeid = " . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			exit;
		}
		$query = "SELECT id,".self::fieldnames.",schoolid,referenceid FROM vw_".self::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new Notice();
        	$object->setId($row[0]);
			$object->setSubject($row[1]);
			$object->setContent($row[2]);
			$object->setCdate($row[3]);
			$object->setPageid($row[4]);
			$object->setSchoolid($row[5]);
			$object->setReferenceid($row[6]);
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
 		$id = $object->getId();
		$subject = $object->getSubject();
		$content = $object->getContent();
		$cdate = $object->getCdate();
		$pageid = $object->getPageid();
		
		$content = str_replace("'", "\'", $content);

//		$schoolid = $object->getSchoolid();
//		$referenceid = $object->getReferenceid();
		$query = "UPDATE ".self::tablename." SET " .
		"subject='$subject', " . 
		"content='$content', " . 
		"pageid='$pageid' "  .
		"WHERE id=$id";
		
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".self::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }

}

class Faculty {
	private $id;
	private $name;
	private $description;
	private $pic;
	private $schoolid;
	private $picname;
	
    public function getDescription()
    {
        return $this->description;
    }
    public function setDescription($description)
    {
        $this->description = $description;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getName()
    {
        return $this->name;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
    public function getPic()
    {
        return $this->pic;
    }
    public function setPic($pic)
    {
        $this->pic = $pic;
    }
    public function getSchoolid()
    {
        return $this->schoolid;
    }
    public function setSchoolid($schoolid)
    {
        $this->schoolid = $schoolid;
    }

    public function getPicname()
    {
        return $this->picname;
    }
    public function setPicname($picname)
    {
        $this->picname = $picname;
    }

}
class FacultyDAO extends DAO {
	const tablename = "faculty";
	const fieldnames = "name,description,pic";
	
    public function add($object)
    {
 		$id = $object->getId();
		$name = $object->getName();
		$description = $object->getDescription();
		$pic = $object->getPic();
		$schoolid = $object->getSchoolid();
		
		$query = "INSERT INTO ".self::tablename." (".self::fieldnames.") VALUES ( " .
		"'$name','$description', $pic )";
		
		$id = $this->ExecuteInsert($query);
		$object->setId($id);

		$query = "INSERT INTO school_faculty (schoolid,facultyid) VALUES ( " .
		"$schoolid,$id )";
		$this->ExecuteInsert($query);
    }
    public function delete($object)
    {
 		$query = "DELETE FROM ".self::tablename." WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$query = "DELETE FROM school_faculty WHERE facultyid=" . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			exit;
		}
		$query = "SELECT id,".self::fieldnames.",schoolid,picname FROM vw_".self::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new Faculty();
        	$object->setId($row[0]);
			$object->setName($row[1]);
			$object->setPic($row[3]);
			$object->setDescription($row[2]);
			$object->setSchoolid($row[4]);
			$object->setPicname($row[5]);
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
 		$id = $object->getId();
		$name = $object->getName();
		$description = $object->getDescription();
		$pic = $object->getPic();
		$schoolid = $object->getSchoolid();
		$query = "UPDATE ".self::tablename." SET " .
		"name='$name', " . 
		"description='$description', " . 
		"pic=$pic "  .
		"WHERE id=$id";
		
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".self::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }

}

class Department {
	private $id;
	private $name;
	private $description;
	private $pic;
	private $facultyid;
	private $picname;

    public function getDescription()
    {
        return $this->description;
    }
    public function setDescription($description)
    {
        $this->description = $description;
    }
    public function getFacultyid()
    {
        return $this->facultyid;
    }
    public function setFacultyid($facultyid)
    {
        $this->facultyid = $facultyid;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getName()
    {
        return $this->name;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
    public function getPic()
    {
        return $this->pic;
    }
    public function setPic($pic)
    {
        $this->pic = $pic;
    }

    public function getPicname()
    {
        return $this->picname;
    }
    public function setPicname($picname)
    {
        $this->picname = $picname;
    }

}
class DepartmentDAO extends DAO {
	const tablename = "department";
	const fieldnames = "name,description,pic";
	
    public function add($object)
    {
 		$id = $object->getId();
		$name = $object->getName();
		$description = $object->getDescription();
		$pic = $object->getPic();
		$facultyid = $object->getFacultyid();
		
		$query = "INSERT INTO ".self::tablename." (".self::fieldnames.") VALUES ( " .
		"'$name','$description', $pic )";
		
		$id = $this->ExecuteInsert($query);
		$object->setId($id);

		$query = "INSERT INTO faculty_department (facultyid,departmentid) VALUES ( " .
		"$facultyid,$id )";
		$this->ExecuteInsert($query);
    }
    public function delete($object)
    {
 		$query = "DELETE FROM ".self::tablename." WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$query = "DELETE FROM faculty_department WHERE departmentid = " . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			echo "[getList]: Invalid condition";
			exit;
		}
		$query = "SELECT id,".self::fieldnames.",facultyid,picname FROM vw_".self::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new Department();
        	$object->setId($row[0]);
			$object->setName($row[1]);
			$object->setDescription($row[2]);
			$object->setPic($row[3]);
			$object->setFacultyid($row[4]);
			$object->setPicname($row[5]);
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
 		$id = $object->getId();
		$name = $object->getName();
		$description = $object->getDescription();
		$pic = $object->getPic();
//		$facultyid = $object->getFacultyid();
		$query = "UPDATE ".self::tablename." SET " .
		"name='$name', " . 
		"description='$description', " . 
		"pic=$pic "  .
		"WHERE id=$id";
		$this->ExecuteUpdate($query);
		
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".self::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }
}

class Subject {
	private $id;
	private $name;
	private $description;
	private $departmentid;
	
    public function getDepartmentid()
    {
        return $this->departmentid;
    }
    public function setDepartmentid($departmentid)
    {
        $this->departmentid = $departmentid;
    }
    public function getDescription()
    {
        return $this->description;
    }
    public function setDescription($description)
    {
        $this->description = $description;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getName()
    {
        return $this->name;
    }
    public function setName($name)
    {
        $this->name = $name;
    }

}
class SubjectDAO extends DAO {
	const tablename = "subject";
	const fieldnames = "name,description";
	const linktable = "department_subject";
	
    public function add($object)
    {
 		$id = $object->getId();
		$name = $object->getName();
		$description = $object->getDescription();
		$departmentid = $object->getDepartmentid();
		
		$query = "INSERT INTO ".self::tablename." (".self::fieldnames.") VALUES ( " .
		"'$name','$description' )";
		$id = $this->ExecuteInsert($query);
		$object->setId($id);

		$query = "INSERT INTO department_subject (departmentid,subjectid) VALUES ( " .
		"$departmentid,$id )";
		$this->ExecuteInsert($query);
    }
    public function delete($object)
    {
 		$query = "DELETE FROM ".self::tablename." WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$query = "DELETE FROM department_subject WHERE subjectid = " . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			error_log ("[getList]: Invalid condition");
			exit;
		}
		$query = "SELECT id,".self::fieldnames.",departmentid FROM vw_".self::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new Subject();
        	$object->setId($row[0]);
			$object->setName($row[1]);
			$object->setDescription($row[2]);
			$object->setDepartmentid($row[3]);
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
 		$id = $object->getId();
		$name = $object->getName();
		$description = $object->getDescription();
//		$departmentid = $object->getDepartmentid();
		$query = "UPDATE ".self::tablename." SET " .
		"name='$name', " . 
		"description='$description' " . 
		"WHERE id=$id";
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".self::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }

}

class Clazz{
	private $id;
	private $pic;
	private $description;
	private $picname;
	private $semesterid;
	private $semester;
	private $subjectid;
	private $timetable;

    public function getDescription()
    {
        return $this->description;
    }
    public function setDescription($description)
    {
        $this->description = $description;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getPic()
    {
        return $this->pic;
    }
    public function setPic($pic)
    {
        $this->pic = $pic;
    }
    public function getPicname()
    {
        return $this->picname;
    }
    public function setPicname($picname)
    {
        $this->picname = $picname;
    }
    public function getSemester()
    {
        return $this->semester;
    }
    public function setSemester($semester)
    {
        $this->semester = $semester;
    }
    public function getSubjectid()
    {
        return $this->subjectid;
    }
    public function setSubjectid($subjectid)
    {
        $this->subjectid = $subjectid;
    }

    public function getSemesterid()
    {
        return $this->semesterid;
    }
    public function setSemesterid($semesterid)
    {
        $this->semesterid = $semesterid;
    }

    public function getTimetable()
    {
        return $this->timetable;
    }
    public function setTimetable($timetable)
    {
        $this->timetable = $timetable;
    }

}
class ClazzDAO extends DAO{
	const tablename = "class";
	const fieldnames = "pic,description";
	
    public function add($object)
    {
		$id = $object->getId();
		$pic = $object->getPic();
		$description = $object->getDescription();
		$picname = $object->getPicname();
		$semesterid = $object->getSemesterid();
		$semester = $object->getSemester();
		$subjectid = $object->getSubjectid();

		$query = "INSERT INTO ".self::tablename." (".self::fieldnames.") VALUES ( " .
		"$pic,'$description' )";
		$id = $this->ExecuteInsert($query);
		$object->setId($id);
		$object->getTimetable()->setClassid($id);
		
		$query = "INSERT INTO subject_class (subjectid,classid) VALUES ( " .
		"$subjectid,$id )";
		$this->ExecuteInsert($query);

		$timetabledao = new TimetableDAO();
		$timetabledao->add($object->getTimetable());

//		$query = "INSERT INTO class_timetable (classid,timetableid) VALUES ( " .
//		"$id, " . $object->getTimetable()->getId() . " )";
//		$this->ExecuteInsert($query);
		
		$query = "INSERT INTO class_semester (classid,semesterid) VALUES ( " .
		"$id,$semesterid )";
		$this->ExecuteInsert($query);
		
    }
    public function delete($object)
    {
		$id = $object->getId();
		// delete Timetable
		$query = "DELETE FROM timetable WHERE id IN (SELECT timetableid FROM class_timetable WHERE classid = $id)";
		$this->ExecuteUpdate($query);
        // delete Class_Timetable
		$query = "DELETE FROM class_timetable WHERE classid = $id";
		$this->ExecuteUpdate($query);
		// delete subject_class
		$query = "DELETE FROM subject_class WHERE classid = $id";
		$this->ExecuteUpdate($query);
		// delete class_semester
		$query = "DELETE FROM class_semester WHERE classid = $id";
		$this->ExecuteUpdate($query);
		// delete Class
		$query = "DELETE FROM class WHERE id = $id";
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			error_log ("[getList]: Invalid condition");
			exit;
		}
		$query = "SELECT id,".self::fieldnames.",picname, semesterid, semester, subjectid FROM vw_".self::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
		$timetabledao = new TimetableDAO();
    	foreach ($result as $row) {
    		$object = new Clazz();
        	$object->setId($row[0]);
			$object->setPic($row[1]);
			$object->setDescription($row[2]);
			$object->setPicname($row[3]);
			$object->setSemesterid($row[4]);
			$object->setSemester($row[5]);
			$object->setSubjectid($row[6]);
			$timetables = $timetabledao->getList("classid=" . $row[0]);
			$object->setTimetable( $timetables[0] );
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
		$id = $object->getId();
		$pic = $object->getPic();
		$description = $object->getDescription();
		$picname = $object->getPicname();
		$subjectid = $object->getSemesterid();
		$semester = $object->getSemester();
		$subjectid = $object->getSubjectid();
		$query = "UPDATE ".self::tablename." SET " .
		"pic=$pic, " . 
		"description='$description' " . 
		"WHERE id=$id";
		
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".self::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }

}

class Timetable{
	private $id;
	private $weekday;
	private $starttime;
	private $endtime;
	private $location;
	private $classid;

    public function getClassid()
    {
        return $this->classid;
    }
    public function setClassid($classid)
    {
        $this->classid = $classid;
    }
    public function getEndtime()
    {
        return $this->endtime;
    }
    public function setEndtime($endtime)
    {
        $this->endtime = $endtime;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getLocation()
    {
        return $this->location;
    }
    public function setLocation($location)
    {
        $this->location = $location;
    }
    public function getStarttime()
    {
        return $this->starttime;
    }
    public function setStarttime($starttime)
    {
        $this->starttime = $starttime;
    }
    public function getWeekday()
    {
        return $this->weekday;
    }
    public function setWeekday($weekday)
    {
        $this->weekday = $weekday;
    }

    public function getDay()
    {
        switch($this->weekday){
        	case 1:
				return 'Monday';
			case 2:
				return 'Tuesday';	
			case 3:
				return 'Wednesday';	
			case 4:
				return 'Thursday';	
			case 5:
				return 'Friday';	
			case 6:
				return 'Saturday';	
			case 7:
				return 'Sunday';	
        }
    }

}
class TimetableDAO extends DAO {
	const tablename = "timetable";
	const fieldnames = "weekday,starttime,endtime,location";
	
    public function add($object)
    {
 		$id = $object->getId();
		$weekday = $object->getWeekday();
		$starttime = $object->getStarttime();
		$endtime = $object->getEndtime();
		$location = $object->getLocation();
		$classid = $object->getClassid();

		$query = "INSERT INTO ".self::tablename." (".self::fieldnames.") VALUES ( " .
		"$weekday,'$starttime','$endtime','$location' )";
		$id = $this->ExecuteInsert($query);
		$object->setId($id);
		
		$query = "INSERT INTO class_timetable (classid,timetableid) VALUES ( " .
		"$classid,$id )";
		$this->ExecuteInsert($query);
		
    }
    public function delete($object)
    {
 		$query = "DELETE FROM ".self::tablename." WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$query = "DELETE FROM class_timetable WHERE timetableid = " . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			error_log ("[getList]: Invalid condition");
			exit;
		}
		$query = "SELECT id,".self::fieldnames.",classid FROM vw_".self::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new Timetable();
        	$object->setId($row[0]);
			$object->setWeekday($row[1]);
			$object->setStarttime($row[2]);
			$object->setEndtime($row[3]);
			$object->setLocation($row[4]);
			$object->setClassid($row[5]);
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
 		$id = $object->getId();
		$weekday = $object->getWeekday();
		$starttime = $object->getStarttime();
		$endtime = $object->getEndtime();
		$location = $object->getLocation();
//		$classid = $object->getClassid();
		$query = "UPDATE ".self::tablename." SET " .
		"weekday=$weekday, " . 
		"starttime='$starttime' " . 
		"endtime='$endtime', " . 
		"location='$location', " . 
		"WHERE id=$id";
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".self::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }

}

class Feedback {
	private $id;
	private $subject;
	private $content;
	private $cdate;
	private $pageid;
	private $referenceid;

    public function getCdate()
    {
        return $this->cdate;
    }
    public function setCdate($cdate)
    {
        $this->cdate = $cdate;
    }
    public function getContent()
    {
        return $this->content;
    }
    public function setContent($content)
    {
        $this->content = $content;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getPageid()
    {
        return $this->pageid;
    }
    public function setPageid($pageid)
    {
        $this->pageid = $pageid;
    }
    public function getReferenceid()
    {
        return $this->referenceid;
    }
    public function setReferenceid($referenceid)
    {
        $this->referenceid = $referenceid;
    }
    public function getSubject()
    {
        return $this->subject;
    }
    public function setSubject($subject)
    {
        $this->subject = $subject;
    }

}
class FeedbackDAO extends DAO {
	const tablename = "feedback";
	const fieldnames = "subject,content,cdate,pageid";
	
	
    public function add($object)
    {
		$subject = $object->getSubject();
		$content = $object->getContent();
		$cdate = $object->getCdate();
		$pageid = $object->getPageid();
		$referenceid = $object->getReferenceid();
		
		$content = str_replace("'", "\'", $content);
		
		$query = "INSERT INTO ".self::tablename." (".self::fieldnames.") VALUES ( " .
		"'$subject','$content', now(), $pageid )";
		
		$id = $this->ExecuteInsert($query);
		$object->setId($id);

		$query = "INSERT INTO feedback_reference(feedbackid,referenceid) VALUES($id,$referenceid)";
		$this->ExecuteInsert($query);
    }
    public function delete($object)
    {
 		$query = "DELETE FROM ".self::tablename." WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$query = "DELETE FROM feedback_reference WHERE feedbackid = " . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			exit;
		}
		$query = "SELECT id,".self::fieldnames.",referenceid FROM vw_".self::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new Notice();
        	$object->setId($row[0]);
			$object->setSubject($row[1]);
			$object->setContent($row[2]);
			$object->setCdate($row[3]);
			$object->setPageid($row[4]);
			$object->setReferenceid($row[5]);
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
 		$id = $object->getId();
		$subject = $object->getSubject();
		$content = $object->getContent();
		$cdate = $object->getCdate();
		$pageid = $object->getPageid();
		$referenceid = $object->getReferenceid();
		
		$content = str_replace("'", "\'", $content);

		$query = "UPDATE ".self::tablename." SET " .
		"subject='$subject', " . 
		"content='$content', " . 
		"pageid='$pageid' "  .
		"WHERE id=$id";
		
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".self::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }

}

class AccessMatrix {
	private $id;
	private $task;
	private $name;
	private $action;
	private $minimumlevel;
	private $owneronly;

    public function getAction()
    {
        return $this->action;
    }
    public function setAction($action)
    {
        $this->action = $action;
    }
    public function getId()
    {
        return $this->id;
    }
    public function setId($id)
    {
        $this->id = $id;
    }
    public function getMinimumlevel()
    {
        return $this->minimumlevel;
    }
    public function setMinimumlevel($minimumlevel)
    {
        $this->minimumlevel = $minimumlevel;
    }
    public function getName()
    {
        return $this->name;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
    public function getOwneronly()
    {
        return $this->owneronly;
    }
    public function setOwneronly($owneronly)
    {
        $this->owneronly = $owneronly;
    }
    public function getTask()
    {
        return $this->task;
    }
    public function setTask($task)
    {
        $this->task = $task;
    }

}
class AccessMatrixDAO extends DAO { 
	const tablename = "accessmatrix";
	const fieldnames = "task,name,action,minimumlevel,owneronly";

    public function add($object)
    {
		$task = $object->getTask();
		$name = $object->getName();
		$action = $object->getAction();
		$minimumlevel = $object->getMinimumlevel();
		$owneronly = $object->getOwneronly();
		
		$query = "INSERT INTO ".self::tablename." (" . self::fieldnames . ") VALUES ( " .
		"'$task','$name', '$action', $minimumlevel, $owneronly )";
		
		$id = $this->ExecuteInsert($query);
		$object->setId($id);
    }
    public function delete($object)
    {
 		$query = "DELETE FROM ".self::tablename." WHERE id=" . $object->getId();
		$this->ExecuteUpdate($query);
		$object = null;
    }
    public function getList($condition)
    {
		$list = array();
		if($condition==null){
			exit;
		}
		$query = "SELECT id,".self::fieldnames." FROM ".self::tablename." WHERE $condition ";

    	$result = $this->ExecuteQuery($query);
    	foreach ($result as $row) {
    		$object = new AccessMatrix();
        	$object->setId($row[0]);
			$object->setTask($row[1]);
			$object->setName($row[2]);
			$object->setAction($row[3]);
			$object->setMinimumlevel($row[4]);
			$object->setOwneronly($row[5]);
 			array_push($list, $object);
    	}
		return $list;
    }
    public function save($object)
    {
		$id = $object->getId();
		$task = $object->getTask();
		$name = $object->getName();
		$action = $object->getAction();
		$minimumlevel = $object->getMinimumlevel();
		$owneronly = $object->getOwneronly();
		
		$query = "UPDATE ".self::tablename." SET " .
		"task='$task', " . 
		"name='$name', " . 
		"action='$action', " . 
		"minimumlevel=$minimumlevel, " . 
		"owneronly=$owneronly "  .
		"WHERE id=$id";
		
		$this->ExecuteUpdate($query);
    }
    public function updateList($statement, $condition)
    {
 		$query = "UPDATE ".self::tablename." SET " . $statement .
		" WHERE " . $condition;
		return $this->ExecuteUpdate($query);
    }

}
?>